﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Runtime.InteropServices.WindowsRuntime;
using System.Runtime.Remoting.Contexts;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Forms;
using static System.Net.Mime.MediaTypeNames;

namespace WindowsFormsApp1
{
    public partial class Form1 : Form
    {
        internal readonly Test Test1 = new Test();
        public static Label myLab;
        private InvokeHelper context;
        private object kk = new object();
        private object bb = new object();
        public Form1()
        {
            InitializeComponent();

            this.Shown += Form1_Shown;
            this.Load += Form1_Load;

            context = new InvokeHelper(SynchronizationContext.Current);

            Test1.MyLab = label1;
        }
        protected override void OnShown(EventArgs e)
        {
            base.OnShown(e);
            Console.WriteLine("Shown1");
        }
        protected override void OnHandleCreated(EventArgs e)
        {
            base.OnHandleCreated(e);
            Console.WriteLine("Handle");
        }
        private void Form1_Load(object sender, EventArgs e)
        {
            Console.WriteLine("Load");

            KK();
        }

        private void Form1_Shown(object sender, EventArgs e)
        {
            Console.WriteLine("Shown");
        }

        protected override void OnCreateControl()
        {
            base.OnCreateControl();
            Console.WriteLine("Create");
        }
        void KK()
        {

            Thread T1 = new Thread(Test);
            T1.SetApartmentState(ApartmentState.STA);
            T1.IsBackground = true;
            T1.Start();

            Thread T2 = new Thread(Test);
            T2.SetApartmentState(ApartmentState.STA);
            T2.IsBackground = true;
            T2.Start();
        }
        void Test()
        {
            while (true)
            {
                try
                {
                    lock (kk)
                    {
                        int ka = 0;
                        int b = 10 / ka;
                    }
                }
                catch (Exception ex)
                {
                    try
                    {
                        lock (bb)
                        {
                            int ka = 0;
                            int b = 10 / ka;
                            string aaaa = $"aaa{ex}";
                            Console.WriteLine(aaaa);
                        }
                    }
                    catch (Exception)
                    {
                        Console.WriteLine("baocuo l ");
                    }
                }

                Console.WriteLine("a");
                Thread.Sleep(1000);

            }
        }
        private void button1_Click(object sender, EventArgs e)
        {

            DataTable dt = new DataTable();
            dt.Columns.Add("a");
            dt.Columns.Add("b");

            List<Parson> parson = new List<Parson>();


            DataRow dataRow = dt.NewRow();
            //dataRow[0] = 1;
            dataRow["b"] = "b-1"; //parson.FirstOrDefault(p => p.Name == "a")?.Age;
            dt.Rows.Add(dataRow);





            byte[] a = new byte[4];
            a[0] = 1;
            a[1] = 2;
            a[2] = 3;
            a[3] = 4;
            string a1 = Convert.ToString(a[0], 2);
            string a2 = Convert.ToString(a[1], 2);
            string a3 = Convert.ToString(a[2], 2);
            string a4 = Convert.ToString(a[3], 2);
            a1 = a1.Insert(0, new string('0', 8 - a1.Length));

            BitArray bitArray = new BitArray(a);

            Process.Start("cmd.exe", "net time");

            //char[] charArray = text.ToCharArray();
            //Array.Reverse(charArray);
            //return new string(charArray);

            //using (StreamWriter streamWriter = new StreamWriter($"D:\\Source\\a.txt", append: true, Encoding.UTF8))
            //{
            //    streamWriter.WriteLine($"{DateTime.Now:HH:mm:ss}: a");
            //}
            //string fileName = "d:\\a.csv";
            //FileMode mode = (FileExist(fileName) ? FileMode.Append : FileMode.Create);

            //using (var fs = new FileStream(fileName, mode, FileAccess.Write, FileShare.Inheritable))
            //{

            //        //写入信息
            //        byte[] bytes = Encoding.Default.GetBytes("asdf");
            //        fs.Write(bytes, 0, bytes.Length);

            //    fs.Flush();
            //    fs.Close();
            //}
        }
        private static bool FileExist(string fileName)
        {
            bool append = false;
            if (File.Exists(fileName))
                append = true;

            string dir = Path.GetDirectoryName(fileName);
            if (!Directory.Exists(dir))
                Directory.CreateDirectory(dir);
            return append;
        }
    }
}
